package main

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	// mysql扩展 https://github.com/go-sql-driver/mysql
	db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/test?charset=utf8")
	if err != nil {
		fmt.Println(err)
	}
	// db连接一般不主动关闭, 除非确认确实不再需要该连接了, go会进行回收
	defer db.Close()
	var name string
	err = db.QueryRow("select name from users where id = ?", 1).Scan(&name)
	if err != nil {
		if err == sql.ErrNoRows {
			//没有行，但也没有错误发生
		} else {
			fmt.Println(err)
		}
	}
	fmt.Println(name)
}
